package com.ruoyi.web.controller.app;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.app.domain.bo.AppImMessageFavoriteBo;
import com.ruoyi.app.domain.vo.AppImMessageFavoriteVo;
import com.ruoyi.app.service.IAppImMessageFavoriteService;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.web.req.AppImMessageFavoriteBoList;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息收藏
 *
 * @author ruoyi
 * @date 2025-11-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/imMessageFavorite")
public class AppImMessageFavoriteController extends BaseController {

    private final IAppImMessageFavoriteService iAppImMessageFavoriteService;

    /**
     * 查询消息收藏列表
     */
    @GetMapping("/list")
    public TableDataInfo<AppImMessageFavoriteVo> list(AppImMessageFavoriteBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        return iAppImMessageFavoriteService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取消息收藏详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<AppImMessageFavoriteVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iAppImMessageFavoriteService.queryById(id));
    }

    /**
     * 新增消息收藏
     */
    @Log(title = "消息收藏", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppImMessageFavoriteBoList bo) {
        if (!CollUtil.isEmpty(bo.getAppImMessageFavoriteBos())){
            for (AppImMessageFavoriteBo appImMessageFavoriteBo : bo.getAppImMessageFavoriteBos()) {
                iAppImMessageFavoriteService.insertByBo(appImMessageFavoriteBo);
            }
        }
        return toAjax(bo.getAppImMessageFavoriteBos().size());
    }



    /**
     * 删除消息收藏
     */
    @Log(title = "删除消息收藏", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return toAjax(iAppImMessageFavoriteService.deleteWithValidByIds(CollUtil.newArrayList(id), true) ? 1 : 0);
    }
}
